<?php

namespace App\Admin\Controllers;

use App\Models\PaymentThirdPay;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PaymentThirdPayController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaymentThirdPay(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('merchant');
            $grid->column('is_open')->switch();
            $grid->column('pay')->switch();
            $grid->column('withdrawal')->switch();
            //$grid->column('pay_code');
            //$grid->column('upper');
            //$grid->column('lower');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->disableFilterButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->showQuickEditButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PaymentThirdPay(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('merchant');
            $show->field('pay_key');
            $show->field('withdrawal_key');
            $show->field('password');
            $show->field('is_open');
            $show->field('pay');
            $show->field('withdrawal');
            $show->field('server_name');
            $show->field('pay_url');
            $show->field('pay_query_url');
            $show->field('withdrawal_url');
            $show->field('withdrawal_query_url');
            $show->field('pay_code');
            $show->field('withdrawal_code');
            $show->field('public_key');
            $show->field('private_key');
            $show->field('upper');
            $show->field('lower');
            $show->field('sort');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaymentThirdPay(), function (Form $form) {

            $is_edit = $form->isEditing();
            if ($is_edit) {
                $form->display('server_name');
            } else {
                //添加
                $form->select('server_name')->options(PaymentThirdPay::PAY_OPTION)->required();
            }

            $form->text('title')->required();
            $form->text('merchant')->required();
            $form->text('pay_key');
            $form->text('withdrawal_key');
            $form->text('password');
            $form->switch('is_open')->default(true);
            $form->switch('pay')->default(true);
            $form->switch('withdrawal')->default(true);

            $form->text('pay_url');
            $form->text('pay_query_url');
            $form->text('withdrawal_url');
            $form->text('withdrawal_query_url');
            //$form->text('pay_code')->help("");
            $form->text('withdrawal_code');
            $form->text('public_key');
            $form->text('private_key');
            //$form->text('upper')->required();
            //$form->text('lower')->required();
            $form->text('sort');
            $form->text('remark');

            $form->saving(function ($form) use ($is_edit) {
                $form->password = $form->password ?: '';
                $form->pay_query_url = $form->pay_query_url ?: '';
                $form->pay_code = $form->pay_code ?: '';
                $form->withdrawal_query_url = $form->withdrawal_query_url ?: '';
                $form->withdrawal_code = $form->withdrawal_code ?: '';
            });


        });
    }
}
